﻿using MoetutuServer.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MoetutuServer.Helper.PublishHelper
{
    class TagHelper
    {

        public static List<RomeTagInfo> ReadTagFile()
        {
            List<RomeTagInfo> TagList = new List<RomeTagInfo>();
            List<string> FileAllLine= FileHelper.ReadAllLine(StaticValue.RomeTagPath);
            foreach (string line in FileAllLine)
            {
                String[] TagSplit = Regex.Split(line, "-------");

                if (TagSplit.Count() == 2) {
                    TagList.Add(new RomeTagInfo() { CnTag1= TagSplit [0].Trim(), RomeTag1= TagSplit[1].Trim()});
                 

                }
            }
            return TagList;
        }
        /// <summary>
        /// 判断一个列表里是否存在另一个列表 存在返回true，不存在返回false
        /// </summary>
        /// <param name="LittleList"></param>
        /// <param name="BigList"></param>
        /// <returns></returns>
        public static bool ExistenceTag(List<string> LittleList, List<string> BigList) {
            foreach (string Line in LittleList) {
                if (BigList.Contains(Line.Trim())) {
                    Console.WriteLine( "捕获到黑名单关键词："+Line);
                    return true;//如果存在
                }
            }
            return false;
        }
        /// <summary>
        /// 去掉乱七八糟的tag
        /// </summary>
        /// <param name="TagList"></param>
        /// <returns></returns>
        public static List<string> TagShort(List<string> TagList) {
            List<string> TempList = new List<string>();
            foreach (string line in TagList) {
       
                string TempText = TextHelper.ReplaceText(line, "[0-9]{0,}users入り", "");
             
                int TAGIMGCount = MoetutuServer.Pixiv.ProcessImg.Helper.GetTagCount(TempText);
                if (TAGIMGCount < 100)
                {
                    Console.WriteLine("{1}图片数量{0},过少不添加。",TAGIMGCount,TempText);
                    continue;
                }
                if (!TempList.Contains(TempText))
                    TempList.Add(TempText);
            }

            return TempList;

        }
        /// <summary>
        /// 获取已翻译过的列表
        /// </summary>
        /// <param name="UntreatedList"></param>
        /// <returns></returns>
        public static List<string> TranslateTag(List<string> UntreatedList) {
            List<string> TempList = UntreatedList;

            for (int i = 0; i < UntreatedList.Count; i++) {
             
                   String ChineseTag = SearchCnTag(UntreatedList[i].Trim());
                if (ChineseTag != "" && !TempList.Contains(ChineseTag))
                {
                    TempList.Add(ChineseTag);
                }
            }
         
            return TempList;
        }
        public static string SearchCnTag(String RomeTag) {
            int Index = -1;
            Index = StaticValue.RomeTagList.FindIndex(delegate (RomeTagInfo romeTag) { return romeTag.RomeTag1 == RomeTag; });
            
            if (Index >= 0) {
                Console.WriteLine("已翻译："+StaticValue.RomeTagList[Index].CnTag1);
             return   StaticValue.RomeTagList[Index].CnTag1;

            }
            return "";
        }
        public struct RomeTagInfo
        {

            string CnTag;
            string RomeTag;
            public string CnTag1 { get => CnTag; set => CnTag = value; }
            public string RomeTag1 { get => RomeTag; set => RomeTag = value; }
        }
    }
}
